Tip Corner: Timing

© 2005, Brad Moore

Liberty Basic Connection

Home

Tip Corner

API Corner

Youth Corner

SpriteByte: Basics

StyleBits 3

Shaped Window

Recursion

Eddie's Lessons

Beginners Programming

Help

Index


Creating a Pseudo-timer

I wanted to take a moment and remind folks that there is more than one way to add timing to a Liberty Basic program. Most people immediately think of the TIMER command when they want to do timing - however there is another way.

The time command allows you to do some simple timing if you need to pause for a specific period of time. Here is what you do presuming you know the duration of your pause:

  1. Get the current time in seconds (or milliseconds)
  2. Add duration to seconds in step 1 - this is your target time in seconds
  3. Start a WHILE loop
  4. repeat as long as current seconds is less than your target seconds (step 2)

The code looks like this:


    'pause for 10 seconds
    sec = 10
    st = time$("seconds")
    while time$("seconds") < sec + st
    wend

This little trick works because Liberty Basic returns the seconds since midnight when you make a call to TIME$("seconds"). Obviously you could run afoul of this little trick if you were using it near midnight. If one thinks this were possible you could build in some error checking. We know that there are only 86400 seconds in a day, so if the target seconds is greater than that, we could fix it by subtracting that value and setting the seconds to the next day. Of course the timing loop would finish early for a few cycles in this case. Try this:


    'pause for 10 seconds
    sec = 10
    st = time$("seconds")
    target = st + sec
    if target > 86399 then target = target - 86399
    while time$("seconds") < target
    wend

Here is nice little demo that shows this timer in action:


    NOMAINWIN
    WindowWidth = 388 : WindowHeight = 255
    UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
    UpperLeftY = INT((DisplayHeight-WindowHeight)/2)

graphicbox  #main.gbx, 20, 50, 235, 150
statictext  #main.st1, "Seconds to pause (1 - 60):", 20, 15, 155, 20
button      #main.go, "Go",[go],UL, 270, 120, 90, 35
button      #main.quit, "Exit",[quit],UL, 270, 165, 90, 35
textbox     #main.tbx, 180, 10, 75, 25

Open "Time Demo" for Window as #main
    #main "trapclose [quit]"
    #main.gbx "down; fill Black; flush"
    #main "font ms_sans_serif 10"
    
    #main.gbx "goto 6 20; color green; backcolor black; font arial 14 bold"
    #main.gbx "\Results:"
    #main.tbx "2"

[loop]
    Wait

[quit]
    close #main : END

[go]
    'place code here
    #main.tbx "!contents? value$"
    sec = val(value$)
    
    if sec < 1 then 
        notice "Error" + chr$(13) + "The seconds specified must be greater than zero"
        wait
    end if

    if sec > 60 then 
        notice "Error" + chr$(13) + "The seconds specified must be 60 or less"
        wait
    end if
    
    #main.gbx "up; goto 6 50;down; font courier_new 12 bold"
    #main.gbx "\Running..."
    
    'this is where the pseudo timer begins
    stime$ = time$()
    st = time$("seconds")
    target = st + sec
    
    if target > 86399 then target = target - 86399

    while time$("seconds") < target
    wend
    
    et = time$("seconds")
    etime$ = time$()
    
    elapsed = et - st
    
    #main.gbx "up; goto 6 50; down; font courier_new 12 bold"
    #main.gbx "\Running... finished!"
    #main.gbx "up; goto 6 75; down"
    #main.gbx "\start time: ";stime$
    #main.gbx "\  end time: ";etime$
    #main.gbx "\   elapsed: ";elapsed
    wait



Home

Tip Corner

API Corner

Youth Corner

SpriteByte: Basics

StyleBits 3

Shaped Window

Recursion

Eddie's Lessons

Beginners Programming

Help

Index